import { Controller, Get, Post, UseGuards, Request, Body, Param } from '@nestjs/common';
import { GroupsService } from './groups.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('groups')
export class GroupsController {
  constructor(private groupsService: GroupsService) {}

  @UseGuards(JwtAuthGuard)
  @Post()
  async createGroup(
    @Request() req,
    @Body() groupData: { name: string; description?: string; isPublic?: boolean },
  ) {
    return this.groupsService.create({
      ...groupData,
      ownerId: req.user.userId,
    });
  }

  @UseGuards(JwtAuthGuard)
  @Get()
  async getUserGroups(@Request() req) {
    return this.groupsService.findByUser(req.user.userId);
  }

  @UseGuards(JwtAuthGuard)
  @Get(':id')
  async getGroup(@Param('id') id: string) {
    return this.groupsService.findById(id);
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/join')
  async joinGroup(@Request() req, @Param('id') groupId: string) {
    return this.groupsService.addMember(groupId, req.user.userId);
  }

  @UseGuards(JwtAuthGuard)
  @Post(':id/leave')
  async leaveGroup(@Request() req, @Param('id') groupId: string) {
    return this.groupsService.removeMember(groupId, req.user.userId);
  }
}
